package com.jenkins.test.web;

import com.jenkins.test.cron.ScheduleBean;
import com.jenkins.test.cron.ScheduleService;
import com.jenkins.test.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @version V1.0.0
 * @ClassName: {@link ScheduleController}
 * @Description: 动态定时任务测试
 * @author: Lanzhou
 * @date: 2021/6/28 14:46
 * @Copyright:2020 All rights reserved.
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;


    @GetMapping("/start")
    public Result<String> start() {
        ScheduleBean bean = new ScheduleBean();
        bean.setKey("abed125");
        bean.setCron("0/10 * * * * *");
        scheduleService.registerJob(bean);
        return Result.success("启动成功 !!");
    }

    @GetMapping("/cancel")
    public Result<String> cancel() {
        scheduleService.stopJob("abed125");
        return Result.success("停止成功 !!");
    }

    @GetMapping("/change")
    public Result<String> change() {
        ScheduleBean bean = new ScheduleBean();
        bean.setKey("abed256");
        bean.setCron("0/5 * * * * *");
        scheduleService.flushJob(bean);
        System.out.println("ScheduleController.change()" + LocalDateTime.now().toString());
        return Result.success("切换成功 !!");
    }


}
